/*
 * RestServer.java
 *
 * Created on 2007-10-18, 00:01:53
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package info.mikrotubula.server;

import org.restlet.Component;
import org.restlet.data.Protocol;
import org.apache.log4j.Logger;
import org.restlet.Application;

/**
 *
 * @author Krystian
 */
public class RestServer {

    public static final Integer PORT_MIN = 1;
    public static final Integer PORT_MAX = 65535;
    public static final Integer PORT_DEFAULT = 8182;
    static Logger logger = Logger.getLogger(RestServer.class);

    public static void main(String[] args) {
        logger.info("Here we go!");
        Integer port;
        try {
            if (args != null && args.length > 0 && Integer.parseInt(args[0]) <= PORT_MAX && Integer.parseInt(args[0]) >= PORT_MIN) {
                port = Integer.parseInt(args[0]);
            } else {
                port = PORT_DEFAULT;
            }
        } catch (NumberFormatException ex) {
            logger.warn("No port specified, using default: " + PORT_DEFAULT);
            port = PORT_DEFAULT;
        }
        Component component = new Component();
        try {
            component.getServers().add(Protocol.HTTP, port);
            Application app = new ImageApplication(component.getContext());
            component.getDefaultHost().attach(app);
            
            logger.info("Starting component");
            component.start();
        } catch (Exception e) {
            logger.error("Component start didn't go that well...");
            e.printStackTrace();
        }
    }
}